package com.example.kotlinExample.config

import com.fasterxml.jackson.annotation.JsonAutoDetect
import com.fasterxml.jackson.annotation.PropertyAccessor
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.jsontype.DefaultBaseTypeLimitingValidator
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import org.springframework.cache.annotation.CachingConfigurerSupport
import org.springframework.cache.annotation.EnableCaching
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.redis.connection.RedisConnectionFactory
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer
import org.springframework.data.redis.serializer.StringRedisSerializer

/**
 *
 *
 * @author liliang
 */
@Configuration
@EnableCaching
class RedisConfig: CachingConfigurerSupport() {

    @Bean
    fun redisTemplate(connectionFactory: RedisConnectionFactory): RedisTemplate<Any, Any> =
        RedisTemplate<Any, Any>().apply {
            setConnectionFactory(connectionFactory)
//            val serializer = Gson2JsonRedisSerializer(Any::class.java)
            val serializer = Jackson2JsonRedisSerializer(Any::class.java)
            valueSerializer = serializer
            hashValueSerializer = serializer
            StringRedisSerializer().apply {
                keySerializer = this
                hashKeySerializer = this

            }

        }
}